import Vue from 'vue'
import VueRouter from 'vue-router'

//1导入路由（登录页面）
import login from '../views/user/login.vue'
import home from '../views/home/home.vue'
import movie from '../views/movie/movie.vue'
import qa from '../views/qa/qa.vue'
import user from '../views/user/user.vue'
import layout from '../views/layout.vue'
import edit from '../views/user/edit.vue'


Vue.use(VueRouter)

const routes = [
  //2 配置路由规则
  {
    name: 'login',
    path: '/login',
    component: login
  },
  {
    name: 'edit',
    path: '/edit',
    component: edit
  },
  {
    name: 'layout',
    path: '/',
    component: layout,
    //二级路由
    children: [{
        name: 'home',
        path: '/home',
        component: home
      },
      {
        name: 'movie',
        path: '/movie',
        component: movie
      },
      {
        name: 'qa',
        path: '/qa',
        component: qa
      },
      {
        name: 'user',
        path: '/user',
        component: user,
        meta: { isLogin:true}
      },
      {
        path:'/',
        redirect:'/home'
      }
    ]

  },

]


const router = new VueRouter({
  routes
})

export default router

//导入 token路由
import {getToken} from '../utils/token.js'
import {Toast} from 'vant';//导入vant组件

// 注册前置路由导航
router.beforeEach((to, from, next) => {
  //不是所有的跳转都需要做登录判断，把需要判断的页面添加meta
    //1 判断这个页面是不是要做登录验证
    if( to.meta.isLogin){
        // 1.1进到这里需要做登录判断  
            // 检查是否登录过 是否有token
              if(getToken() ){
                //2.1 有登录 有token值 通过跳转
                  next()

              }else{
                Toast.fail('您还没有登录，请先登录')
                next('/login')
              }
    }else{
      //  1.2 不需要做登录判断 直接跳转
      next()
    }
})